package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 * @Description MD5工具类
 * @Author 冷彭磊
 * @Data 2025/2/26 8:55
 */


public class Md5Utils {
    public static String calculateMd5(File file) throws IOException, NoSuchAlgorithmException {
        try (FileInputStream inputStream = new FileInputStream(file)) {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                digest.update(buffer, 0, bytesRead);
            }
            byte[] md5Bytes = digest.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : md5Bytes) {
                sb.append(String.format("%02x", b));
            }
//             String md5= sb.toString().toUpperCase();
//            System.out.println("计算获得的MD5:" + md5);
            return sb.toString().toUpperCase();
        }
    }
}




